package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.PTypeDao;
import com.aaa.javaweb.dao.SheShiDao;
import com.aaa.javaweb.dao.TRukuDao;
import com.aaa.javaweb.entity.SheShi;
import com.aaa.javaweb.entity.TRuku;
import com.aaa.javaweb.until.PageInfo;
import com.aaa.javaweb.until.TransBean;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class TRukudetailController {
    @Autowired
    private TRukuDao rukuDao;
    @Autowired
    private PTypeDao pTypeDao;
    @Autowired
    private SheShiDao sheShiDao;
    @RequestMapping("/getSheShiType")
    public List<Map> getAllType(){
        List<Map> list = pTypeDao.selectALL();
        return list;
    }

    @RequestMapping("/getBookIdAndNameByTypeId")
    public List<SheShi> getBookIdAndNameByTypeId(String ptypeid) {
        List<SheShi> list = sheShiDao.getBookIdAndNameByTypeId(Integer.parseInt(ptypeid));
        return list;
    }
    @RequestMapping("/getBookCountAndPriceById")
    public SheShi getBookCountAndPriceById(String ssid) {
        return sheShiDao.getBookCountAndPriceById(ssid);
    }
    @RequestMapping("/addPut")
    public String ruku(HttpServletRequest request) {
        Map map = (Map) JSON.parse(request.getParameter("data"));
        // 将接收的入库表格数据 转化成 List<Map> 用于添加入库详情单 和 让图书数量增加
        List<Map> tableData = (List<Map>) JSON.parse(request.getParameter("tableData"));
        int rukuid = rukuDao.addRuku(map);
        if (rukuid > 0) {
            // 添加入库详情表
            int result = rukuDao.addRukuDetail(tableData, rukuid);
            if (result > 0) {
                // 增加商品的数量,返回影响的总行数
                int i = rukuDao.rukuAndAddBookCount(tableData);
                return i + "";
            }
        }
        return "0";
    }
    @RequestMapping("/getAllRuku")
    public PageInfo<TRuku> getAllRuku(TRuku ruku){
        PageInfo<TRuku> info = new PageInfo<>();
        long count = rukuDao.getCount(ruku);
        if (count > 0) {
            info.setCount(count);
            List<TRuku> allRuku = rukuDao.getAllRuku(ruku);
            info.setData(allRuku);
        }
        return info;
    }
    @RequestMapping("/getAllRukuDetailByRukuId")
    public PageInfo<Map> getAllRukuDetailByRukuId(String rukuid) {
        PageInfo<Map> info = new PageInfo<>();
        info.setCount(0L);
        List<Map> list = rukuDao.getAllRukuDetailByRukuId(rukuid);
        if (list.size() > 0) {
            info.setData(list);
        }
        return info;
    }
}
